//
//  homeCore.m
//  HHNMobileCore
//
//  Created by huhmf on 15/7/24.
//  Copyright (c) 2015年 huhmf. All rights reserved.
//

#import "homeCore.h"
#import <AFHTTPSessionManager.h>

#import <MJExtension.h>
#import "ZxInfo.h"
#import "ZXInfoRequest.h"
#import "LawLibCategoryListRequest.h"
#import "ConvenienceListRequest.h"
#import "InteractionListRequest.h"
@interface homeCore() <IHomeClient>


@end
@implementation homeCore



-(void) requestZxInfoWithFid:(NSString *) fid  andPage:(NSString *) page andPageSize:(NSString *) pageSize andKeywords:(NSString*) keywords SuccBlock:(void(^)(ZxInfo *zxInfoObject)) succBlock failBlock:(void(^)(NSString* message)) failBlock {
    
    ZXInfoRequest *api = [[ZXInfoRequest alloc] initRequestWithFid:fid andPage:page andPageSize:pageSize andKeywords:keywords];
    
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSError *err;
        HHLog(@"result = %@",request.responseString);
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:request.responseString.mj_JSONData
                                                             options:NSJSONReadingMutableContainers
                                                               error:&err];
        if(err) {
            LogError(THome, @"requestProductListApi,json to dict error：%@", err.localizedDescription);
            failBlock([NSString stringWithFormat:@"json to dict error：%@",err.localizedDescription]);
            return;
        }
        
        HHLog(@"requestProductListApi,Succ isDataFromCache:%d",[api isDataFromCache]);
        
        succBlock([self dictionaryToZxInfoObject:dict]);
        
    } failure:^(YTKBaseRequest *request) {
        LogError(THome, @"requestProductListApi error:%@", request.requestTask.error.localizedDescription);
        failBlock(request.requestTask.error.localizedDescription);
    }];
    
    
}

-(void) requestLawLibCategoryListWithCname:(NSString *)cname andCid:(NSString *)cid SuccBlock:(void(^)(LawLibrary *lawLibrayObject)) succBlock failBlock:(void(^)(NSString *msg)) failBlock{
    
    
    LawLibCategoryListRequest *api =[[LawLibCategoryListRequest alloc] initRequestWithCname:cname andCid:cid];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSError *err;
        HHLog(@"result = %@",request.responseString);
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:request.responseString.mj_JSONData
                                                             options:NSJSONReadingMutableContainers
                                                               error:&err];
        if(err) {
            LogError(THome, @"requestProductListApi,json to dict error：%@", err.localizedDescription);
            failBlock([NSString stringWithFormat:@"json to dict error：%@",err.localizedDescription]);
            return;
        }
        
        HHLog(@"requestProductListApi,Succ isDataFromCache:%d",[api isDataFromCache]);
        
        succBlock([self dictionaryToLawLibray:dict]);
        
    } failure:^(YTKBaseRequest *request) {
        LogError(THome, @"requestProductListApi error:%@", request.requestTask.error.localizedDescription);
        failBlock(request.requestTask.error.localizedDescription);
    }];
}


-(void) requestConvenienceListWithCid:(NSString *) cid SuccBlock:(void(^)(ConvenienceInfo *convenienceInfo)) succBlock failBlock:(void(^)(NSString *msg)) failBlock {
    
    ConvenienceListRequest *api = [[ConvenienceListRequest alloc] initRequestWith:cid];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSError *err;
        HHLog(@"result = %@",request.responseString);
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:request.responseString.mj_JSONData
                                                             options:NSJSONReadingMutableContainers
                                                               error:&err];
        if(err) {
            LogError(THome, @"requestProductListApi,json to dict error：%@", err.localizedDescription);
            failBlock([NSString stringWithFormat:@"json to dict error：%@",err.localizedDescription]);
            return;
        }
        
        HHLog(@"requestProductListApi,Succ isDataFromCache:%d",[api isDataFromCache]);
        
        succBlock([self dictionaryToConvenienceInfo:dict]);
        
    } failure:^(YTKBaseRequest *request) {
        LogError(THome, @"requestProductListApi error:%@", request.requestTask.error.localizedDescription);
        failBlock(request.requestTask.error.localizedDescription);
    }];
    
}

// 警民互动
-(void) requestInteractionListWith:(NSString *) userName andPage:(NSString *)page andPageSize:(NSString *)pageSize andKeywords:(NSString *) keywords SuccBlock:(void(^)(InteractionInfo *interactionInfo)) succBlock failBlock:(void(^)(NSString *msg)) failBlock {
    
    InteractionListRequest *api = [[InteractionListRequest alloc] initRequestWithUserName:userName andPage:page andPageSize:pageSize andKeywords:keywords];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSError *err;
        HHLog(@"result = %@",request.responseString);
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:request.responseString.mj_JSONData
                                                             options:NSJSONReadingMutableContainers
                                                               error:&err];
        if(err) {
            LogError(THome, @"requestProductListApi,json to dict error：%@", err.localizedDescription);
            failBlock([NSString stringWithFormat:@"json to dict error：%@",err.localizedDescription]);
            return;
        }
        
        HHLog(@"requestProductListApi,Succ isDataFromCache:%d",[api isDataFromCache]);
        
        succBlock([self dictionaryToInteractionInfo:dict]);
        
    } failure:^(YTKBaseRequest *request) {
        LogError(THome, @"requestProductListApi error:%@", request.requestTask.error.localizedDescription);
        failBlock(request.requestTask.error.localizedDescription);
    }];
    
    
    
}
-(ZxInfo *) dictionaryToZxInfoObject:(NSDictionary*) dict {
    
    if (dict==nil) {
        return nil;
    }
    
    
    if([[dict valueForKey:@"data"] isKindOfClass:[NSDictionary class]])
    {
        
        NSDictionary *dict_data=[dict valueForKey:@"data"];
        
        [ZxInfo mj_setupReplacedKeyFromPropertyName:^NSDictionary * {
            return @{
                     @"zxInfos":@"zxInfo",
                     @"xfInfos":@"xfInfo",
                     @"bannerInfos":@"bannerInfo",
                     @"adInfos":@"adInfo",
                     @"scrollInfos":@"scrollInfo",
                     };
            
        }];
        [ZxInfo mj_setupObjectClassInArray:^NSDictionary *{
            return @{
                     @"zxInfos":[ZxInfoObject class],
                     @"xfInfos":[ZxInfoObject class],
                     @"bannerInfos":[ZxInfoObject class],
                     @"adInfos":[ZxInfoObject class],
                     @"scrollInfos":[ZxInfoObject class],
                     };
        }];
        
        [ZxInfoObject mj_setupReplacedKeyFromPropertyName:^NSDictionary * {
            
            return @{
                     @"infoId":@"id",
                     };
            
        }];
        
        [ZxInfoObject mj_setupObjectClassInArray:^NSDictionary *{
            
            return @{
                     @"albums":[Albums class],
                     @"fields":[Fields class],
                     @"attach":[Attach class],
                     @"goods":[Goods class]
                     };
            
            
        }];
        
        [Albums mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
            
            
            return @{
                     @"albumsId":@"id"
                     };
            
        }];
        
        ZxInfo *list=[ZxInfo mj_objectWithKeyValues:dict_data];
        return list;
        
        
    }else{
        LogError(THome, @"dictionary to object error:data is not dictionary");
        return nil;
    }
    
}


-(LawLibrary *) dictionaryToLawLibray:(NSDictionary *) dict {
    
    if (dict==nil) {
        return nil;
    }
    
    
    if([[dict valueForKey:@"data"] isKindOfClass:[NSArray class]])
    {
        [LawLibrary mj_setupReplacedKeyFromPropertyName:^NSDictionary * {
            return @{
                     @"lawLibraryObjects":@"data"
                     };
            
        }];
        [LawLibrary mj_setupObjectClassInArray:^NSDictionary *{
            return @{
                     @"lawLibraryObjects":[LawLibraryObject class]
                     };
        }];
        
        [LawLibraryObject mj_setupReplacedKeyFromPropertyName:^NSDictionary * {
            
            return @{
                     @"lawId":@"id",
                     };
            
        }];
        
        [LawLibraryObject mj_setupObjectClassInArray:^NSDictionary *{
            
            return @{
                     @"childList":[LawLibraryObject class]
                     };
            
            
        }];
        
        
        LawLibrary *list=[LawLibrary mj_objectWithKeyValues:dict];
        return list;
        
        
    }else{
        LogError(THome, @"dictionary to object error:data is not dictionary");
        return nil;
    }

    
}


-(ConvenienceInfo *) dictionaryToConvenienceInfo:(NSDictionary *) dict {
    
    
    if (dict==nil) {
        return nil;
    }
    
    if([[dict valueForKey:@"data"] isKindOfClass:[NSArray class]])
    {
        [ConvenienceInfo mj_setupReplacedKeyFromPropertyName:^NSDictionary * {
            return @{
                     @"convenienceInfos":@"data"
                     };
            
        }];
        [ConvenienceInfo mj_setupObjectClassInArray:^NSDictionary *{
            return @{
                     @"convenienceInfos":[ConvenienceObject class]
                     };
        }];
        
        [ConvenienceObject mj_setupReplacedKeyFromPropertyName:^NSDictionary * {
            
            return @{
                     @"convenienceId":@"id",
                     };
            
        }];
        
        [ConvenienceObject mj_setupObjectClassInArray:^NSDictionary *{
            
            return @{
                     @"childList":[ConvenienceObject class]
                     };
            
            
        }];
        
        
        ConvenienceInfo *list=[ConvenienceInfo mj_objectWithKeyValues:dict];
        return list;
        
        
    }else{
        LogError(THome, @"dictionary to object error:data is not dictionary");
        return nil;
    }

}




-(InteractionInfo *) dictionaryToInteractionInfo:(NSDictionary *) dict {
    
    if (dict==nil) {
        return nil;
    }
    
    if([[dict valueForKey:@"data"] isKindOfClass:[NSDictionary class]])
    {
        NSDictionary *dataDict = [dict valueForKey:@"data"];
        [InteractionInfo mj_setupReplacedKeyFromPropertyName:^NSDictionary * {
            return @{
                     @"list":@"list",
                     @"totalcount":@"totalcount"
                     };
            
        }];
        [InteractionInfo mj_setupObjectClassInArray:^NSDictionary *{
            return @{
                     @"list":[InteractionObject class]
                     };
        }];
        
        [InteractionObject mj_setupReplacedKeyFromPropertyName:^NSDictionary * {
            
            return @{
                     @"interactionId":@"id",
                     };
            
        }];
        
        [InteractionObject mj_setupObjectClassInArray:^NSDictionary *{
            
            return @{
                     @"albums":[NSString class]
                     };
            
            
        }];
        
        
        InteractionInfo *list=[InteractionInfo mj_objectWithKeyValues:dataDict];
        return list;
        
        
    }else{
        LogError(THome, @"dictionary to object error:data is not dictionary");
        return nil;
    }

    
}
@end
